//
//  KKBLEConfig.m
//
//  Created by zorro on 2017/11/13.
//  Copyright © 2017年 zorro. All rights reserved.
//

#import "KKBLEConfig.h"
#import "KKBLEService.h"
#import "KKBLESendI6.h"
#import "KKBLEAcceptI6.h"
@implementation KKBLEConfig

+ (KKBLEConfig *)configWithModel:(KKBLEModel *)model {
    KKBLEConfig *config = [[KKBLEConfig alloc] init];
    
    [config parseDeviceModel:model];
    [config parseDeviceProtocol:model.bleName];
    
    if (model.bleAdvID == 0) {
        NSInteger adv = [model.bleMAC getIntValue];
        if(adv >0)
        {
            [LASTCONNECTBLEHEAD setIntValue:adv];
            model.bleAdvID = adv;
        }
    }
    
    [config parseDeviceFunction:model.bleAdv];
    return config;
}

- (void)parseDeviceModel:(KKBLEModel *)model {
    _model = model;
}

+ (NSArray *)X9 {
    return @[@"X9", @"SOne", @"X1pro"];
}

+ (NSArray *)I6 {
    return @[@"I6"];
}

+ (NSArray *)deviceNames {
    NSMutableArray *arr = [[NSUserDefaults standardUserDefaults] objectForKey:@"DeviceNameArr"];
    return arr;
}

+ (NSArray *)deviceAdvs
{
    NSMutableArray *arr = [[NSUserDefaults standardUserDefaults] objectForKey:@"DeviceadvArr"];
    return arr;
}

+ (NSArray *)deviceServices {
    return @[KK_BLEServiceX9, KK_BLEServiceI6, KK_BLEServiceUpdate, KK_BLEServiceTW64,KK_BLEServiceUpdateNRF82,KK_BLEServiceUpdateFJW];
}

+ (NSArray *)deviceUpdateServices
{
    return @[ KK_BLEServiceUpdate,KK_BLEServiceUpdateNRF82,KK_BLEServiceUpdateFJW];
}

// 解析协议
- (void)parseDeviceProtocol:(NSString *)name {
    _protocol = KKBLEProtocolX9;
    NSArray *array = [KKBLEConfig I6];
    name = [name lowercaseString];
    for (int i = 0; i < array.count; i++) {
        NSString *tmp = [array[i] lowercaseString];
        if ([name isEqualToString:tmp]) {
            _protocol = KKBLEProtocolI6;
            break;
        }
    }
    _bltName = name;
    [self configService];
    [self configSendAndAccept];
    [self configDeviceType:name];
}

- (void)configDeviceType:(NSString *)name {
    if ([name isEqualToString:DEVICEX9]) {
        _deviceType = BLTModelDeviceX9;
    } else if ([name isEqualToString:DEVICEI6]) {
        _deviceType = BLTModelDeviceI6;
    } else if ([name isEqualToString:DEVICEIONE]) {
        _deviceType = BLTModelDeviceSOne;
    } else if ([name isEqualToString:DEVICEX10] || [KK_ShareData compareWithString:name andCompareWithOtherString:DEVICEX10] || [name isEqualToString:DEVICEX1]) {
        _deviceType = BLTModelDeviceX10;
    }
}

- (BLTModelDeviceType)deviceType {
    if ([_bltName isEqualToString:DEVICEX9]) {
        return BLTModelDeviceX9;
    } else if ([_bltName isEqualToString:DEVICEI6]) {
        return BLTModelDeviceI6;
    } else if ([_bltName isEqualToString:DEVICEIONE]) {
        return BLTModelDeviceSOne;
    } else if ([_bltName isEqualToString:DEVICEX10] || [KK_ShareData compareWithString:_bltName andCompareWithOtherString:DEVICEX10] || [_bltName isEqualToString:DEVICEX1]) {
        return BLTModelDeviceX10;
    }
    
    return BLTModelDeviceX9;
}

- (void)configService {
    if (_protocol == KKBLEProtocolX9) {
        _service = [CBUUID UUIDWithString:@"8f400001-cfb4-14a3-f1ba-f61f35cddbaf"];
        _write   = [CBUUID UUIDWithString:@"8f400002-cfb4-14a3-f1ba-f61f35cddbaf"];
        _notify  = [CBUUID UUIDWithString:@"8f400003-cfb4-14a3-f1ba-f61f35cddbaf"];
        
        _updateService = [CBUUID UUIDWithString:@"00001530-1212-EFDE-1523-785FEABCD123"];
        _updateControl = [CBUUID UUIDWithString:@"00001531-1212-EFDE-1523-785FEABCD123"];
        _updatePacket  = [CBUUID UUIDWithString:@"00001532-1212-EFDE-1523-785FEABCD123"];
        _updateVersion = [CBUUID UUIDWithString:@"00001534-1212-EFDE-1523-785FEABCD123"];
    } else {
        _service = KK_BLEServiceI6;
        _write   = [CBUUID UUIDWithString:@"0xfff1"];
        _notify  = [CBUUID UUIDWithString:@"0xfff1"];
    }
}

// 配置发送和接收类
- (void)configSendAndAccept {
    if (_protocol == KKBLEProtocolX9) {
        KK_BLEService.send   = [[KKBLESend alloc] init];
        KK_BLEService.accept = [[KKBLEAccept alloc] init];
    } else {
        KK_BLEService.send   = [[KKBLESendI6 alloc] init];
        KK_BLEService.accept = [[KKBLEAcceptI6 alloc] init];
    }
}

// 解析设备功能与MAC地址
- (void)parseDeviceFunction:(NSData *)adv {
    if (_protocol == KKBLEProtocolX9) {
        NSString *string   = [[NSString alloc] initWithData:adv encoding:NSUTF8StringEncoding];
        NSString *string16 = [NSString stringWithFormat:@"0x%@", string];
        // 转换为16进制
        unsigned long Number = strtoul([string16 UTF8String], 0, 16);
        // 初始为NO
        SN_USER.heart           = NO;
        SN_USER.airPressure     = NO;
        SN_USER.oxygen          = NO;
        SN_USER.bistoryPressure = NO;
        SN_USER.ancs            = NO;
        if (string.length == 4) {
            NSLog(@"%@ 广播包 转换完的数字为：%lx",_model.bleName,Number);
            [LASTCONNECTBLEHEAD setIntValue:Number];
        } else {
            Number = [LASTCONNECTBLEHEAD getIntValue];
        }
        NSLog(@"%@ 广播包>>>>>>%d",_model.bleName,Number);
        if (Number == 0) {
            NSString *info = [NSString stringWithFormat:@"%@%@",SN_Text(@"错误!"),SN_Text(@"解除设备绑定")];
            SHOWMBProgressHUD(info, nil, nil, NO, 2.0);
        }
        
        [self updateService:(int)Number];
        [SN_Net getDeviceSFromServerWithSuccess:nil fail:nil];
    }
    if (_protocol == KKBLEProtocolI6) {
    }
}

- (void)updateService:(int)adv {
    [LASTCONNECTBLEHEAD setIntValue:adv];
    KK_BLEModel.bleAdvID    = adv;
    SN_USER.heart           = intConvertBit(adv, 0);
    SN_USER.airPressure     = intConvertBit(adv, 1);
    SN_USER.oxygen          = intConvertBit(adv, 2);
    SN_USER.bistoryPressure = intConvertBit(adv, 3);
    SN_USER.ancs            = intConvertBit(adv, 4);
    NSLog(@"获取广播包解析硬件功能>>>%i",SN_USER.heart);
    [KK_BLEModel updateToDB];
    [SN_USER updateToDB];
}

+ (BOOL)isFitDevice:(NSString *)name {
    BOOL    isFit  = NO;
    NSArray *array = [KKBLEConfig deviceNames];
    name = [name lowercaseString];
    for (int i = 0; i < array.count; i++) {
        NSString *deviceName = array[i];
        NSString *tmp        = [deviceName lowercaseString];
        if ([name isEqualToString:tmp]) {
            isFit = YES;
            break;
        }
    }
    return isFit;
}

+(BOOL)isFitAdv:(NSString *)adv
{
    BOOL    isFit  = NO;
    NSArray *array = [KKBLEConfig deviceAdvs];
    for (int i = 0; i < array.count; i++) {
        NSString *deviceName = array[i];
        if ([adv isEqualToString:deviceName]) {
            isFit = YES;
            break;
        }
    }
    return isFit;
}

+ (NSArray *)uartServiceUUIDAndUpdateService {
    return @[[CBUUID UUIDWithString:@"8F400001-CFB4-14A3-F1BA-F61F35CDDBAF"], [CBUUID UUIDWithString:@"0xfff0"], [CBUUID UUIDWithString:@"00001530-1212-EFDE-1523-785FEABCD123"]];
}

@end

